/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rsds;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import sun.awt.VerticalBagLayout;

/**
 *
 * @author ahmet
 */
public class SubsystemContainerPanel extends JPanel{
    private JScrollPane scrollPane;
    private GridLayout gl;
    private JPanel container;

    private Vector<SubsystemAddPanel> subsystems;
    
    private JButton addNewButton;

    private SubsystemContainerPanel self;

    public SubsystemContainerPanel() {
        super();

        self = this;

        setLayout(new BorderLayout());

        container = new JPanel();
        scrollPane = new JScrollPane(container);

        add(scrollPane, BorderLayout.CENTER);

        subsystems = new Vector<SubsystemAddPanel>();
        SubsystemAddPanel sp1 = new SubsystemAddPanel(self, subsystems, 1);
        subsystems.add(sp1);

        gl = new GridLayout();
        gl.setRows(2);
        gl.setColumns(1);

        container.setLayout(gl);

        addNewButton = new JButton("Add new");
        addNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                int ssLength = subsystems.size();
                SubsystemAddPanel sp = new SubsystemAddPanel(self, subsystems, ssLength+1);
                subsystems.add(sp);
                fillContainer();
            }
        });

        fillContainer();
    }

    public void fillContainer() {
        // add items to the container panel
        // set gl accordingly

        int ssLength = subsystems.size();
        //gl.setRows(ssLength+1); // + 1 for addnewbutton

        container.removeAll();
        //container.setLayout(gl);
        Box bv = Box.createVerticalBox();
        for(int i=0; i<ssLength; i++) {
            //container.add(subsystems.get(i));
            bv.add(subsystems.get(i));
        }
        //System.out.println(subsystems.size());
        //container.add(addNewButton);
        bv.add(addNewButton);
        container.setLayout(new BorderLayout());
        container.add(bv, BorderLayout.NORTH);
        container.updateUI();
    }

    public Vector<SubsystemAddPanel> getSubsystems() {
        return subsystems;
    }
}
